package org.hackystat.iw.projectoverviewer.page.treemap;

/*
 * This code is highly inspired by Object Lab.
 * 
 * www.ObjectLab.co.uk
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JToolTip;
import net.sf.jtreemap.swing.JTreeMap;
// uncomment for debug purpose
//import net.sf.jtreemap.swing.TreeMapNode;

/**
 * The tool tip to display while the user selects a file on the treemap.
 * 
 * @author Laurent DUTHEIL
 * @author Jianfei Liao
 * @author Nathan Britton 
 */
public class IWToolTip extends JToolTip {
  private static final long serialVersionUID = -4399040954147014670L;

  private JTreeMap jTreeMap;

  private static final int TOOLTIP_OFFSET = 5;

  private static final int DEFAULT_VALUE_SIZE = 10;

  private static final int DEFAULT_LABEL_SIZE = 12;

  private Font labelFont;

  private Font valueFont;

  private String weightPrefix;

  private String valuePrefix;

  private boolean showWeight;

  /**
   * Constructor.
   * 
   * @param treeMap The JTreeMap instance.
   * @param weightPrefix The prefix for weight.
   * @param valuePrefix The prefix for value.
   * @param showWeight Shows the weight if true, false otherwise.
   */
  public IWToolTip(JTreeMap treeMap, String weightPrefix, String valuePrefix, boolean showWeight) {
    this.jTreeMap = treeMap;
    this.weightPrefix = weightPrefix;
    this.valuePrefix = valuePrefix;
    this.showWeight = showWeight;
    this.labelFont = new Font("Default", Font.BOLD, DEFAULT_LABEL_SIZE);
    this.valueFont = new Font("Default", Font.PLAIN, DEFAULT_VALUE_SIZE);

    final int width = 160;
    final int height = getFontMetrics(this.labelFont).getHeight()
        + getFontMetrics(this.valueFont).getHeight();

    final Dimension size = new Dimension(width, height);
    this.setSize(size);
    this.setPreferredSize(size);
  }

  /**
   * Override paint method.
   * 
   * @param g The Graphics g.
   */
  @Override
  public void paint(final Graphics g) {
    if (this.jTreeMap.getActiveLeaf() != null) {
      Graphics g2D = g;
      g2D.setColor(Color.YELLOW);
      g2D.fill3DRect(0, 0, this.getWidth(), this.getHeight(), true);
      g2D.setColor(Color.black);
      g2D.setFont(this.labelFont);
      g2D.drawString(this.jTreeMap.getActiveLeaf().getLabel(), TOOLTIP_OFFSET, g2D.getFontMetrics(
          this.labelFont).getAscent());
      g2D.setFont(this.valueFont);
      String toDraw = this.jTreeMap.getActiveLeaf().getLabelValue();
      //**** uncomment for debug purpose, also need to uncomment the corresponding import
      /*TreeMapNode node = this.jTreeMap.getActiveLeaf();
      System.out.println(node.getLabel() + " | " + node.getValue().getLabel() + " | "
          + node.getValue().getValue());*/
      //****
      if (valuePrefix != null) {
        toDraw = valuePrefix + " " + toDraw;
      }
      if (showWeight) {
        toDraw = this.jTreeMap.getActiveLeaf().getWeight() + ", " + toDraw;
      }
      if (weightPrefix != null && showWeight) {
        toDraw = weightPrefix + " " + toDraw;
      }
      g2D.drawString(toDraw, TOOLTIP_OFFSET, this.getHeight() - TOOLTIP_OFFSET);
    }
  }

}
